@file:OptIn(ExperimentalGlideComposeApi::class)

package com.zd.composedemo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import com.bumptech.glide.integration.compose.placeholder
import com.zd.composedemo.ui.theme.ComposeDemoTheme
import com.zd.composedemo.ui.viewmodel.MainViewModel
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

    private val viewModel: MainViewModel by viewModels()

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposeDemoTheme {
                val baseModifier = Modifier.fillMaxSize()
                Box(modifier = baseModifier) {
                    Spacer(baseModifier.background(Color.Green))
                    Box(
                        modifier = baseModifier
                            .safeDrawingPadding()
                            .background(Color.LightGray)
                    ) {
                        HPager()
                    }
                }

                /* Scaffold(
                     topBar = {
                         TopAppBar(
                             colors = topAppBarColors(
                                 containerColor = MaterialTheme.colorScheme.primaryContainer,
                                 titleContentColor = MaterialTheme.colorScheme.primary,
                             ),
                             title = {
                                 TopLayout("首页")
                             }
                         )
                     }, modifier = Modifier.fillMaxSize()
                 ) { innerPadding ->

                     WindowInsets.safeContent
                     Column(modifier = Modifier.padding(innerPadding)) {
 //                        TopLayout("首页")
 ////                        ColumnLayout()
 //                        var clickTime by remember { mutableStateOf<Int>(0) }
 //
 //                        val scope = rememberCoroutineScope()
 //                        val lazyListState =
 //                            rememberLazyListState() // State hoisted to the ConversationScreen
 //
 //                        ClickButton(clickTime) {
 //                            clickTime = System.currentTimeMillis().toInt()
 //                            //顶部添加
 //                            viewModel.itemDatas.add(
 //                                0,
 //                                MessageBean(clickTime, "addOne$clickTime", "饿了么￥")
 //                            )
 //                            //滚动到顶部
 //                            scope.launch {
 //                                lazyListState.scrollToItem(0)
 //                            }
 //                        }


                         //
 //                        HelloContent()
 //
 //
 ////                        ItemLayout(
 ////                            MessageBean(
 ////                                "你想要的小红书",
 ////                                "精彩世界精彩纷呈，你说你是不是就学废了是不是就学废了是不是就学废了是不是就学废了是不是就学废了是不是就学废了是不是就学废了是不是就学废了"
 ////                            )
 ////                        )
 //
 //                        ListItemsLayout(viewModel.itemDatas, lazyListState)
 //
 //
 ////                        MessageListLayout(viewModel.itemDatas, lazyListState, Modifier.weight(1f))
 ////                        Button(onClick = {
 ////                            scope.launch { lazyListState.scrollToItem(0) }
 ////                        }) { Text("topClick") }
 //
 //                        LocalContext.current.resources.getColor(
 //                            R.color.purple_200,
 //                            resources.newTheme()
 //                        )

                         HPager()

                     }
                 }*/
            }
        }
    }
}


@Composable
fun HPager() {
    val pageCount =
        rememberPagerState(
            initialPage = 0, //初始选中
            initialPageOffsetFraction = 0f,
            pageCount = { 5 })

    LaunchedEffect(pageCount) {
        snapshotFlow { pageCount.currentPage }.collect {
            Log.e(">>>>>", "HPager: --> $it")
        }
    }

    Box() {
        val scope = rememberCoroutineScope()

        HorizontalPager(pageCount, modifier = Modifier.fillMaxSize()) { page: Int ->
            GlideImage(
                model = R.drawable.img_start, contentDescription = null,
                loading = placeholder(R.drawable.ic_launcher_background),
                modifier = Modifier
                    .fillMaxSize()
                    .padding(20.dp)
            )
        }

        Row(modifier = Modifier.align(Alignment.BottomCenter)) {
            repeat(pageCount.pageCount) {
                Box(
                    modifier = Modifier
                        .size(20.dp)
                        .padding(3.dp)
                        .clip(CircleShape)
                        .background(
                            if (it == pageCount.currentPage) Color.Black else Color.Gray
                        )
                        .clickable {
                            scope.launch {
                                pageCount.scrollToPage(it)
                            }
                        }
                )
            }
        }
    }

}